﻿

var P_ID = "", TaskID1, TaskID2, t, waiting = false, c = 0, loop = 0, postdate; //声明为全局变量 
//isPreview标志位1预览，0打印
function PrintContent(data, isPreview, IsPrintCard) {
    //LODOP.PRINT_DESIGN();
    if (isPreview) {
        LODOP.PRINT_INIT("");
        //LODOP.SET_PREVIEW_WINDOW(1, 3, 0, 800, 600, "");
        SetPrintContent(data);
        LODOP.PREVIEW();
        //LODOP.SET_PRINT_MODE("CATCH_PRINT_STATUS", true);
        //LODOP.SET_PREVIEW_WINDOW(1, 3, 0, 800, 600, "");
        //if (LODOP.CVERSION) {
        //    LODOP.On_Return = function (TaskID, Value) { document.getElementById('S1').value = Value; };
        //    LODOP.PREVIEW();
        //    return;
        //} else document.getElementById('S1').value = LODOP.PREVIEW();
    }
    else {
        c = 0;
        loop = 0;
        postdate = data;
        if (IsPrintCard) {
            PrintCardWaitLoop();
        }
        else {
            PrintWaitLoop();
        }
        //for (var j = 0; j < data.length; j++) {
        //    PrintContent5(data[j]);
        //    PrintContent5(data[j]);
        //    //LODOP.SET_PRINT_MODE("CATCH_PRINT_STATUS", true);
        //    //if (LODOP.CVERSION) {
        //    //    LODOP.On_Return = function (TaskID, Value) { document.getElementById('S1').value = Value; };
        //    //    LODOP.PRINT();
        //    //    return;
        //    //} else document.getElementById('S1').value = LODOP.PRINT();
        //}
    }
}

function PrintWaitLoop() {
    if (!waiting) {
        if (loop >= postdate.length * 2) {
            return;
        }
        var aa = postdate[parseInt(loop / 2)];
        var batchno = aa.APPORDERNO;
        LODOP.PRINT_INIT("打印订单批次号:" + batchno);
        SetPrintContent(postdate[parseInt(loop / 2)]);
        //SetPrintContent(postdate[loop]);
        if (LODOP.CVERSION) {
            LODOP.On_Return_Remain = true;
            LODOP.On_Return = function (TaskID, Value) {
                if (TaskID == TaskID1) {
                    P_ID = Value;
                    if ((P_ID != "")) { waiting = true; c = 0; loop = loop + 1; setTimeout("PrintWaitLoop()", 200); } else {
                        //document.getElementById('T14').value = "已停止循环！";
                    };
                } else
                    if (TaskID == TaskID2) {
                        if (Value == 0) waiting = false;
                        setTimeout("PrintWaitLoop()", 200);
                    };
            };
            TaskID1 = LODOP.PRINT();
        } else {
            P_ID = LODOP.PRINT();
            if ((P_ID != "")) { waiting = true; c = 0; loop = loop + 1; setTimeout("PrintWaitLoop()", 200); } else {
                //document.getElementById('T14').value = "已停止循环！";
            }
        };
    } else {
        c = c + 1;
        //document.getElementById('T14').value = "正打印第" + loop + "个任务(JOB代码" + P_ID + ")等待：" + c / 5 + "秒";
        if (LODOP.CVERSION) {
            TaskID2 = LODOP.GET_VALUE("PRINT_STATUS_EXIST", P_ID);
        } else {
            if (!LODOP.GET_VALUE("PRINT_STATUS_EXIST", P_ID)) waiting = false;//以是否还在队列为准
            setTimeout("PrintWaitLoop()", 200);
        };
    }

}
function PrintCardWaitLoop() {
    if (!waiting) {
        if (loop >= postdate.length) {
            return;
        }
        var aa = postdate[parseInt(loop)];
        var batchno = aa.APPORDERNO;
        LODOP.PRINT_INIT("打印订单批次号:" + batchno);
        SetPrintContent(postdate[parseInt(loop)]);

        //SetPrintContent(postdate[loop]);
        if (LODOP.CVERSION) {
            LODOP.On_Return_Remain = true;
            LODOP.On_Return = function (TaskID, Value) {
                if (TaskID == TaskID1) {
                    P_ID = Value;
                    if ((P_ID != "")) { waiting = true; c = 0; loop = loop + 1; setTimeout("PrintCardWaitLoop()", 200); } else {
                        //document.getElementById('T14').value = "已停止循环！";
                    };
                } else
                    if (TaskID == TaskID2) {
                        if (Value == 0) waiting = false;
                        setTimeout("PrintCardWaitLoop()", 200);
                    };
            };
            TaskID1 = LODOP.PRINT();
        } else {
            P_ID = LODOP.PRINT();
            if ((P_ID != "")) { waiting = true; c = 0; loop = loop + 1; setTimeout("PrintCardWaitLoop()", 200); } else {
                //document.getElementById('T14').value = "已停止循环！";
            }
        };
    } else {
        c = c + 1;
        //document.getElementById('T14').value = "正打印第" + loop + "个任务(JOB代码" + P_ID + ")等待：" + c / 5 + "秒";
        if (LODOP.CVERSION) {
            TaskID2 = LODOP.GET_VALUE("PRINT_STATUS_EXIST", P_ID);
        } else {
            if (!LODOP.GET_VALUE("PRINT_STATUS_EXIST", P_ID)) waiting = false;//以是否还在队列为准
            setTimeout("PrintCardWaitLoop()", 200);
        };
    }

}

function PrintContent5(ob) {

    LODOP.PRINT_INIT("");
    SetPrintContent(ob);
    LODOP.SET_PRINT_MODE("CATCH_PRINT_STATUS", true);
    if (LODOP.CVERSION) {
        LODOP.On_Return = function (TaskID, Value) { document.getElementById('S1').value = Value; };
        LODOP.PRINT();
        return;
    } else document.getElementById('S1').value = LODOP.PRINT();

    //LODOP.PRINT();
}

function SetPrintContent(ob) {
    LODOP.NewPage();
    LODOP.ADD_PRINT_TEXT("8mm", "20mm", "58mm", "5.61mm", ob.APPORDERNO);
    LODOP.SET_PRINT_STYLEA(0, "FontName", "黑体");
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 16);
    LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
    LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
    LODOP.ADD_PRINT_TEXT("9mm", "3mm", "50mm", "6mm", ob.POINTNAME);
    LODOP.SET_PRINT_STYLEA(0, "FontName", "黑体");
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
    //LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
    LODOP.ADD_PRINT_TEXT("2mm", "3mm", "55mm", "20mm", ob.DISTRICT);
    LODOP.SET_PRINT_STYLEA(0, "FontName", "黑体");
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 18);
    LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
    LODOP.ADD_PRINT_BARCODE("14mm", "28mm", "50mm", "14mm", "128Auto", ob.ORDERNO);
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
    LODOP.ADD_PRINT_TEXT("28mm", "3mm", "70mm", "6mm", ob.CONSIGNEEADD);
    LODOP.SET_PRINT_STYLEA(0, "FontName", "黑体");
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
    LODOP.ADD_PRINT_TEXT("38mm", "3mm", "40mm", "5.2mm", ob.RATIONDAY + ob.RATIONTIME);
    LODOP.SET_PRINT_STYLEA(0, "FontName", "黑体");
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
    LODOP.ADD_PRINT_TEXT("38mm", "38mm", "45mm", "4.3mm", ob.PAYTYPE + " 应收金额:¥" + ob.ORDERAMOUNT);
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
    LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
    LODOP.ADD_PRINT_TEXT("43mm", "3mm", "19.4mm", "4.3mm", ob.CONSIGNEE);
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
    LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
    LODOP.ADD_PRINT_TEXT("43mm", "34mm", "31.8mm", "4.3mm", ob.MOBILE);
    LODOP.SET_PRINT_STYLEA(0, "FontName", "黑体");
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
    LODOP.ADD_PRINT_TEXT("49mm", "3mm", "15mm", "4.3mm", "票号:");
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
    LODOP.ADD_PRINT_TEXT("48mm", "10mm", "58mm", "4.6mm", ob.TICKETNO);
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 21);
    LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
    LODOP.ADD_PRINT_TEXT("48mm", "65mm", "25mm", "4.3mm", ob.RATIONTIME_NOTE);
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 21);
    LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
    LODOP.ADD_PRINT_TEXT("56mm", "4mm", "37mm", "4.3mm", "备注：" + ob.NOTE);
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
    LODOP.ADD_PRINT_TEXT("57mm", "40mm", "39mm", "4.3mm", "客户签名：__________ ");
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
    LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
    LODOP.ADD_PRINT_TEXT("63mm", "3mm", "78mm", "4mm", "订单号:" + ob.SHEETID + " " + ob.CONSIGNEE + " " + ob.MOBILE);
    LODOP.SET_PRINT_STYLEA(0, "FontName", "黑体");
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 9);
    LODOP.ADD_PRINT_TEXT("67mm", "3mm", "35mm", "4mm", ob.RATIONDAY + " " + ob.RATIONTIME);
    LODOP.SET_PRINT_STYLEA(0, "FontName", "黑体");
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 7);
    LODOP.ADD_PRINT_TEXT("67mm", "38mm", "45mm", "4mm", ob.RATIONTIME_NOTE + " " + ob.PAYTYPE + " 应付金额：¥" + ob.ORDERAMOUNT);
    LODOP.SET_PRINT_STYLEA(0, "FontName", "黑体");
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 6);
    LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
    LODOP.ADD_PRINT_TEXT("70mm", "3mm", "72mm", "2.5mm", ob.CONSIGNEEADD);
    LODOP.SET_PRINT_STYLEA(0, "FontName", "黑体");
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 7);
    var arryAll = ob.SaleOrderItem;
    var len = arryAll.length + ob.DIFFQTY;
    var fontsize = 10;
    var startheight = 76;
    var inthigh = 68 / 10;
    if (len <= 30) {
        LODOP.ADD_PRINT_TEXT("72.5mm", "6mm", "15mm", "4mm", "品名");
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
        LODOP.ADD_PRINT_TEXT("72.5mm", "48mm", "20mm", "4mm", "合计数:" + ob.TOTALQTY);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
        LODOP.ADD_PRINT_TEXT("72.5mm", "68mm", "15mm", "4mm", "数量");
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
        fontsize = 10;
        inthigh = 69 / 20;
        if (len <= 20) {
            fontsize = 10;
            inthigh = 69 / 20;
        }
        else {
            fontsize = 7;
            inthigh = 69 / len;
        }
        for (var i = 0; i < arryAll.length ; i++) {
            LODOP.ADD_PRINT_TEXT(startheight + "mm", "4mm", "68mm", inthigh + "mm", arryAll[i].PRODUCTNAME);
            LODOP.SET_PRINT_STYLEA(0, "FontSize", fontsize);
            LODOP.ADD_PRINT_TEXT(startheight + "mm", "68mm", "10mm", inthigh + "mm", arryAll[i].QTY);
            LODOP.SET_PRINT_STYLEA(0, "FontSize", fontsize);
            startheight += inthigh;
            if (arryAll[i].SKUNAME != "" && arryAll[i].PRODUCTNAME != "" && arryAll[i].SKUNAME != arryAll[i].PRODUCTNAME) {
                LODOP.ADD_PRINT_TEXT(startheight + "mm", "4mm", "68mm", inthigh + "mm", "--" + arryAll[i].SKUNAME);
                LODOP.SET_PRINT_STYLEA(0, "FontSize", fontsize);
                startheight += inthigh;
            }
        }
    }
    else {
        LODOP.ADD_PRINT_TEXT("8mm", "10mm", "20mm", "5.61mm", "cb");
        LODOP.ADD_PRINT_TEXT("72.5mm", "6mm", "15mm", "4mm", "品名");
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
        LODOP.ADD_PRINT_TEXT("72.5mm", "33mm", "15mm", "4mm", "数量");
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
        LODOP.ADD_PRINT_TEXT("72.5mm", "39mm", "15mm", "4mm", "品名");
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
        LODOP.ADD_PRINT_TEXT("72.5mm", "48mm", "20mm", "4mm", "合计数:" + ob.TOTALQTY);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
        LODOP.ADD_PRINT_TEXT("72.5mm", "68mm", "15mm", "4mm", "数量");
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
        if (len <= 20) {
            fontsize = 10;
            inthigh = 69 / 10;
        }
        else if (len <= 40) {
            fontsize = 8;
            inthigh = 69 / 20;
        }
        else if (len <= 60) {
            fontsize = 7;
            inthigh = 69 / 30;
        }
        else {
            fontsize = 6;
            inthigh = 69 / len * 2;
        }
        var detailIndex = 0;
        for (var i = 0; i < arryAll.length ; i++) {
            if (detailIndex < len / 2) {
                LODOP.ADD_PRINT_TEXT(startheight + detailIndex * inthigh + "mm", "4mm", "33mm", inthigh + "mm", arryAll[i].PRODUCTNAME);
                LODOP.SET_PRINT_STYLEA(0, "FontSize", fontsize);
                LODOP.ADD_PRINT_TEXT(startheight + detailIndex * inthigh + "mm", "35mm", "6mm", inthigh + "mm", arryAll[i].QTY);
                LODOP.SET_PRINT_STYLEA(0, "FontSize", fontsize);
                detailIndex++;
                if (arryAll[i].SKUNAME != "" && arryAll[i].PRODUCTNAME != "" && arryAll[i].SKUNAME != arryAll[i].PRODUCTNAME) {
                    if ((detailIndex + 1) < len / 2) {
                        LODOP.ADD_PRINT_TEXT(startheight + detailIndex * inthigh + "mm", "4mm", "33mm", inthigh + "mm", "--" + arryAll[i].SKUNAME);
                        LODOP.SET_PRINT_STYLEA(0, "FontSize", fontsize);
                    }
                    else {
                        LODOP.ADD_PRINT_TEXT(startheight + (detailIndex - len / 2) * inthigh + "mm", "38mm", "34mm", inthigh + "mm", "--" + arryAll[i].SKUNAME);
                        LODOP.SET_PRINT_STYLEA(0, "FontSize", fontsize);
                    }
                    detailIndex++;
                }
            }
            else {
                LODOP.ADD_PRINT_TEXT(startheight + (detailIndex - len / 2) * inthigh + "mm", "38mm", "34mm", inthigh + "mm", arryAll[i].PRODUCTNAME);
                LODOP.SET_PRINT_STYLEA(0, "FontSize", fontsize);
                LODOP.ADD_PRINT_TEXT(startheight + (detailIndex - len / 2) * inthigh + "mm", "70mm", "6mm", inthigh + "mm", arryAll[i].QTY);
                LODOP.SET_PRINT_STYLEA(0, "FontSize", fontsize);
                detailIndex++;
                if (arryAll[i].SKUNAME != "" && arryAll[i].PRODUCTNAME != "" && arryAll[i].SKUNAME != arryAll[i].PRODUCTNAME) {
                    LODOP.ADD_PRINT_TEXT(startheight + (detailIndex - len / 2) * inthigh + "mm", "38mm", "34mm", inthigh + "mm", "--" + arryAll[i].SKUNAME);
                    LODOP.SET_PRINT_STYLEA(0, "FontSize", fontsize);
                    detailIndex++;
                }
            }

            //if (arryAll[i].SKUNAME != "" && arryAll[i].PRODUCTNAME != "" && arryAll[i].SKUNAME != arryAll[i].PRODUCTNAME) {
            //    LODOP.ADD_PRINT_TEXT(startheight + "mm", "4mm", "33mm", inthigh + "mm", arryAll[i].GOODSNAME);
            //    LODOP.SET_PRINT_STYLEA(0, "FontSize", fontsize);
            //    LODOP.ADD_PRINT_TEXT(startheight + "mm", "35mm", "6mm", inthigh + "mm", arryAll[i].QTY);
            //    LODOP.SET_PRINT_STYLEA(0, "FontSize", fontsize);
            //}
            //if (i % 2 == 0) {

            //    LODOP.ADD_PRINT_TEXT(startheight + "mm", "4mm", "33mm", inthigh + "mm", arryAll[i].GOODSNAME);
            //    LODOP.SET_PRINT_STYLEA(0, "FontSize", fontsize);
            //    LODOP.ADD_PRINT_TEXT(startheight + "mm", "35mm", "6mm", inthigh + "mm", arryAll[i].QTY);
            //    LODOP.SET_PRINT_STYLEA(0, "FontSize", fontsize);
            //} else {
            //    LODOP.ADD_PRINT_TEXT(startheight + "mm", "38mm", "34mm", inthigh + "mm", arryAll[i].GOODSNAME);
            //    LODOP.SET_PRINT_STYLEA(0, "FontSize", fontsize);
            //    LODOP.ADD_PRINT_TEXT(startheight + "mm", "70mm", "6mm", inthigh + "mm", arryAll[i].QTY);
            //    LODOP.SET_PRINT_STYLEA(0, "FontSize", fontsize);
            //    startheight += inthigh;
            //}

        }
    }
    //LODOP.SET_PRINT_PAGESIZE(1, 800, 1450, "");
}